package com.yh.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yh.bean.Meetingapply;
import com.yh.bean.Meetingmanage;
import com.yh.service.impl.MeetService;

@Controller
public class MeetingOperate {
	
	@Autowired
	@Qualifier("msi")
	private MeetService ms;
	
	//新增会议室
	@RequestMapping("/addmeeting")
	@ResponseBody
	public int addmeeting(Meetingmanage mm){
		System.out.println("新增会议室"+mm.toString());
		int i=ms.addmeeting(mm);
		return i;
	}
	//查询会议室
	@RequestMapping("/querymeeting")
	@ResponseBody
	public Map querymeeting(int curr,int pagesize){
		//进行分页
		Page page=PageHelper.startPage(curr, pagesize);
		
		List<Meetingmanage> mlist=ms.querymeeting();
		long totle=page.getTotal();
		Map map=new HashMap();
		map.put("mlist", mlist);
		map.put("totle", totle);
		return map;
	}
	
	//修改会议室
	@RequestMapping("/updatemeeting")
	@ResponseBody
	public int updatemeeting(Meetingmanage mm){
		int i=ms.updatemeeting(mm);
		return i;
	}
	//删除会议室
	@RequestMapping("/delmeeting")
	@ResponseBody
	public int delmeeting(String mm_ids){
		System.out.println("删除会议室"+mm_ids);
		String[] mmids=mm_ids.split(",");
		int a=0;
	
			a=ms.delmeeting(mmids);
			System.out.println(mmids);
		
		return a;
	}
	//查询可用会议室
	@RequestMapping("/querymeetingtime")
	@ResponseBody
	public Map querymeetingtime(Meetingapply ma,int curr,int pagesize){
		System.out.println("查询会议室"+ma.toString());
		String starttime=null;
		//如果用户搜索时输入的起始时间为空，则默认查询起始时间为当前时间的会议室
		if(ma.getMa_starttime()==null){
			System.out.println("starttime="+ma.getMa_starttime());
			Date date = new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy年M月d日H时m分");
			starttime=sdf.format(date);
			ma.setMa_starttime(starttime);	
		}
		ma.setMa_status("待审核");
		//进行分页
		Page page=PageHelper.startPage(curr, pagesize);
		List<Meetingmanage> mlist=ms.querymeetingtime(ma);
		long totle=page.getTotal();
		Map map=new HashMap();
		map.put("mlist", mlist);
		map.put("totle", totle);
		return map;
	}
	
	//按条件查询预定的会议室
		@RequestMapping("/querymeetingapply")
		@ResponseBody
		public Map querymeetingapply(Meetingapply ma,int curr,int pagesize){
			System.out.println("根据条件查询会议室"+ma.toString());
			
			List<Meetingapply> malistall=ms.querymeetingapply(ma);
			
			//进行分页
			Page page=PageHelper.startPage(curr, pagesize);
			
			List<Meetingapply> malist=ms.querymeetingapply(ma);
			//获取总数据数
			long totle=page.getTotal();
			//获取总页数
			int pages=page.getPages();
			System.out.println("pages="+pages);
		
			//将结果存到model模型中传回前台
			Map map=new HashMap();
			map.put("malist", malist);
			map.put("malistall", malistall);
			map.put("totle", totle);
			map.put("pages", pages);
			return map;
		}
		
		//申请会议室
		@RequestMapping("/Applymeeting")
		@ResponseBody
		public int Applymeeting(Meetingapply ma){
			ma.setMa_status("待审核");
			Date date =new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy年M月d日H时m分");
			String ss=sdf.format(date);
			ma.setMa_applytime(ss);
			System.out.println(ma.toString());
			int i=ms.applymeeting(ma);
			return i;
		}
		//审核会议室
		@RequestMapping("/checkmeeting")
		@ResponseBody
		public int checkmeeting(Meetingapply ma){
			int i=ms.checkmeeting(ma);
			return i;
		}
		//批量删除预定的会议室
		@RequestMapping("/delApply")
		@ResponseBody
		public int delApply(String maids){
			System.out.println("批量取消预定"+maids);
			int a=0;
			String[] ma_ids=maids.split(",");
			for(int i=0;i<ma_ids.length;i++){
				int ma_id=Integer.parseInt(ma_ids[i].toString());
				a=ms.delApply(ma_id);
				System.out.println(ma_id);
				a*=1;
			}
			return a;
		}
}
